package com.eorchis.module.auditing.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.eorchis.core.basedao.dao.impl.HibernateAbstractBaseDao;
import com.eorchis.core.basedao.query.condition.IPagingQueryCondition;
import com.eorchis.core.basedao.query.condition.builder.CompareType;
import com.eorchis.core.basedao.query.condition.builder.IConditionBuilder;
import com.eorchis.core.basedao.query.condition.builder.impl.DefaultQueryConditionBuilder;
import com.eorchis.core.basedao.query.mapper.MapperType;
import com.eorchis.core.basedao.query.mapper.ResultMapperFactory;
import com.eorchis.core.basedao.query.mapper.impl.BeanResultMapper;

import com.eorchis.core.ui.commond.IQueryCommond;

import com.eorchis.module.auditing.ui.commond.AuditingQueryCommond;
import com.eorchis.core.basedao.entity.IBaseEntity;
import com.eorchis.module.auditing.dao.IAuditingDao;
import com.eorchis.module.auditing.domain.Auditing;
import com.eorchis.module.auditing.domain.AuditingBean;
import com.eorchis.utils.utils.PropertyUtil;

/**
 * Title: AuditingDaoImpl<br>
 * Description: 班级审核人员信息数据接口实现<br>
 * Company: GoldGov<br>
 * Copyright @ 2012 GoldGov .All rights reserved.<br>
 * @author Max
 * @createDate 2013-12-31
 * @version $Revision$
 */
@SuppressWarnings("unchecked")
@Repository(value = "com.eorchis.module.auditing.dao.impl.AuditingDaoImpl")
public class AuditingDaoImpl
					extends HibernateAbstractBaseDao
					implements IAuditingDao {
	
	public Class<? extends IBaseEntity> entityClass() {
		return Auditing.class;
	}
	
	public void queryConditionProcessor(
			IConditionBuilder defaultQueryConditionBuilder,
			IQueryCommond queryCommond) {
		AuditingQueryCommond auditingQueryCommond = (AuditingQueryCommond)queryCommond;
		defaultQueryConditionBuilder.setBaseQueryString("SELECT t FROM Auditing t");
		defaultQueryConditionBuilder.addCondition("t.auditingId", CompareType.IN, auditingQueryCommond.getSearchAuditingIds());
		defaultQueryConditionBuilder.addCondition("t.auditingId", CompareType.EQUAL,auditingQueryCommond.getSearchAuditingId());
		
		defaultQueryConditionBuilder.setQueryStringType(QueryStringType.HQL);
	}
	
	/**
	 * 查询审核信息
	 * @param queryCommondst
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2013-12-31
	 */
	public List<AuditingBean> findAuditingInfo(AuditingQueryCommond queryCommond)
	{
		StringBuffer hql=new StringBuffer();
		hql.append("select a.auditingUser.userName as userName,a.auditingDate as auditingStateDate,a.auditingState as auditingState," +
				"a.auditingOpinion as auditingOpinion from Auditing a where 1=1");
		Map<String, Object> params=new HashMap<String, Object>();
		
		if(PropertyUtil.objectNotEmpty(queryCommond.getSearchClassUserId()))
		{
			hql.append(" and a.classUser.classUserId = :classUserId");
			params.put("classUserId", queryCommond.getSearchClassUserId());
		}
		if(PropertyUtil.objectNotEmpty(queryCommond.getAuditingType()))
		{
			hql.append(" and a.auditingType = :auditingType");
			params.put("auditingType", queryCommond.getAuditingType());
		}
		
		hql.append(" order by a.auditingDate desc");
		
		IConditionBuilder defaultQueryConditionBuilder  = new DefaultQueryConditionBuilder();
		defaultQueryConditionBuilder.setBaseQueryString(hql.toString());
		defaultQueryConditionBuilder.setQueryStringType(QueryStringType.HQL);
		IPagingQueryCondition queryCondition = (IPagingQueryCondition) defaultQueryConditionBuilder.buliderQueryCondition();
		queryCondition.setParameters(params);
		
		BeanResultMapper mapper = (BeanResultMapper) ResultMapperFactory.getInstance().getMapper(MapperType.Bean);
		mapper.setResultClass(AuditingBean.class);
		
		queryCondition.setResultMapper(mapper);
		
		List<AuditingBean> auditingBeanList = (List<AuditingBean>) this.findList(queryCondition);
		
		return auditingBeanList;
	}
}
